#include "MoveableItem.h"

#include <QPainter>
#include <QMouseEvent>
#include <QPaintEvent>

MoveableItem::MoveableItem(QWidget* parent) :
	BaseItem(parent)
{
	_moveable = true;
	setMouseTracking(true);
}

void MoveableItem::paintEvent(QPaintEvent *event)
{
	QPainter painter(this);
	painter.setBrush(QBrush(QColor(90,50,140)));
	painter.drawRect(0,0,_width,_height);
	painter.end();
}

void MoveableItem::mousePressEvent(QMouseEvent*event)
{
	_pressed = true;
	_prevPos = event->globalPos();
	emit mouseDown();
}

void MoveableItem::mouseReleaseEvent(QMouseEvent*event)
{
	_pressed = false;
	emit mouseUp();
}

void MoveableItem::mouseMoveEvent(QMouseEvent*event)
{
	if(!_moveable) return;
	if(!_pressed) return;
	int dx = event->globalPos().x() - _prevPos.x();
	int dy = event->globalPos().y() - _prevPos.y();

	_x0 += dx;
	_y0 += dy;

	setPosition(_x0,_y0);

	_prevPos = event->globalPos();

	repaint();
	emit mouseMove();
}

